﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Kaixin001.Common
{
    public class ExceptionHelper
    {
        static string exceptionFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Exception");

        static ExceptionHelper()
        {
            if (!Directory.Exists(exceptionFolder))
            {
                Directory.CreateDirectory(exceptionFolder);
            }
        }

        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="exceptionMsg"></param>
        public static void SaveLog(string exceptionMsg)
        {
            string fileName = DateTime.Now.ToString("yyyyMMdd") + ".txt";
            try
            {
                File.AppendAllText(Path.Combine(exceptionFolder, fileName), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss: ") + exceptionMsg + "\r\n\r\n", Encoding.UTF8);
            }
            catch (Exception e)
            {
            }
        }

        /// <summary>
        /// 记录异常日志
        /// </summary>
        /// <param name="MethodName"></param>
        /// <param name="ex"></param>
        public static void SaveLog(string MethodName,Exception ex)
        {
            string fileName = DateTime.Now.ToString("yyyyMMdd") + ".txt";
            try
            {
                File.AppendAllText(Path.Combine(exceptionFolder, fileName), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + MethodName + ":" + ex.Message + "\r\n\r\n", Encoding.UTF8);
            }
            catch (Exception e)
            {
            }
        }
    }
}
